//
//  ServiceMediator.m
//  Pitch
//
//  Created by zhujinhui on 14-9-11.
//  Copyright (c) 2014年 zhujinhui. All rights reserved.
//

#import "ServiceMediator.h"
#import "NetworkAccess.h"
#import "JsonHandler.h"
#import "NSString+Util.h"
#import "Model.h"

@interface ServiceMediator ()
@end

@implementation ServiceMediator

-(id)initWithService:(NSString *)service delegate:(id<ServiceMediatorDelegate>) delegate{
    if (self = [super init]) {
        self.serviceName = service;
        self.mDelegate = delegate;
    }
    return self;
}

-(NetworkResponse *)findImageList{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NetworkResponse * response = [access doHttpRequest:URL_FINDIMAGELIST andParams:nil];
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}


-(NetworkResponse *)doLogin:(NSString *)userAcct
                   password:(NSString *)userPwd{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userAcct"] = userAcct;
        params[@"userPwd"] = [userPwd md5];
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_DOLOGIN andParams:params];
    if (0 == response.errorCode) {
        [JsonHandler convertToObjectWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

-(NetworkResponse *)doRegisterWithUserAcct:(NSString *) userAcct userPwd:(NSString *) userPwd validateCode:(NSString *)validateCode{
    
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userAcct"]     = userAcct;
        params[@"userPwd"]      = [userPwd md5];
        params[@"validateCode"] = validateCode;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_DOREGISTER andParams:params];
    if (0 == response.errorCode) {
        [JsonHandler convertToObjectWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

-(NetworkResponse *) doSmsCodeWithUserAcct:(NSString *) userAcct type:(NSString *)type{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userAcct"]     = userAcct;
        params[@"type"]         = type;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_DOSMSCODE andParams:params];
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}



-(NetworkResponse *)findOfflineWorkWithInfoTypeId:(NSString *) infoTypeId
                                           cityId:(NSString *) cityId
                                         countyId:(NSString *) countyId
                                        pageIndex:(NSString *) pageIndex
                                         pageSize:(NSString *) pageSize{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    if (infoTypeId) {
        params[@"infoTypeId"]   = infoTypeId;
    }
    if (cityId) {
        params[@"cityId"]       = cityId;
    }
    if (countyId) {
        params[@"countyId"]     = countyId;
    }
    @try {
        params[@"pageIndex"]    = pageIndex;
        params[@"pageSize"]     = pageSize;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_FINDOFFLINEWORK andParams:params];
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    return response;
    
}


-(NetworkResponse *)myPublishListWithUserId:(NSString *) userId
                                  pageIndex:(NSString *) pageIndex
                                   pageSize:(NSString *)pageSize{
    
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]       = userId;
        params[@"pageIndex"]    = pageIndex;
        params[@"pageSize"]     = pageSize;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
        
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_MYPUBLISHLIST andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
    
}

-(NetworkResponse *)findExperienceShareWithUserId:(NSString *)userId
                                        pageIndex:(NSString *)pageIndex
                                         pageSize:(NSString *)pageSize{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]       = userId;
        params[@"pageIndex"]    = pageIndex;
        params[@"pageSize"]     = pageSize;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
        
    }
    NetworkResponse * response = [access doHttpRequest:URL_FINDEXPERIENCESHARE andParams:params];
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    return response;
}


-(NetworkResponse *)findExperienceShareEvalWithExperienceId:(NSString *)experienceId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"experienceId"]     = experienceId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_FINDEXPERIENCESHAREEVAL andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    return response;
}

-(NetworkResponse *)addExprShareEvalWithUserId:(NSString *)userId content:(NSString *)content experienceId:(NSString *) experienceId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]           = userId;
        params[@"content"]          = content;
        params[@"experienceId"]     = experienceId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_ADDEXPRSHAREEVAL andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    return response;
}

-(NetworkResponse *)findSignupStudentWithWorkId:(NSString *)workId businessId:(NSString *) businessId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"workId"]           = workId;
        params[@"businessId"]       = businessId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_FINDSIGNUPSTUDENT andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    return response;
}


-(NetworkResponse *)updateUserCbtnWithUserIdCbtn:(NSString *) userIdCbtn
                                          workId:(NSString *)workId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userIdCbtn"]       = userIdCbtn;
        params[@"workId"]           = workId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_UPDATEUSERCBTN andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    return response;
}


-(NetworkResponse *)myShouldHireListWithUserId:(NSString *) userId
                                     pageIndex:(NSString *)pageIndex
                                      pageSize:(NSString *)pageSize{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]       = userId;
        params[@"pageIndex"]    = pageIndex;
        params[@"pageSize"]     = pageSize;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_MYSHOULDHIRELIST andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
    
}

-(NetworkResponse *)findStudentInfoWithUserId:(NSString *)userId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]       = userId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_FINDSTUDENTINFO andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToObjectWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}




-(NetworkResponse *)findIncomeExpnWithUserId:(NSString *)userId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]       = userId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_FINDINCOMEEXPN andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToListWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

-(NetworkResponse *)updateOnlineSignupWithUserId:(NSString *)userId workId:(NSString *)workId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]       = userId;
        params[@"workId"]       = workId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_UPDATEONLINESIGNUP andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

-(NetworkResponse *)addOfflineWorkWithWork:(Work *)work{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"work"]       = work;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_ADDOFFLINEWORK andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

-(NetworkResponse *)updateStudentInfoWithStudent:(Student *)student{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"student"]       = student;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_UPDATESTUDENTINFO andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}


-(NetworkResponse *)addExprShareWithUserId:(NSString *)userId content:(NSString *)content{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]           = userId;
        params[@"content"]          = content;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_ADDEXPRSHARE andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

/**
 * @return user
 */
-(NetworkResponse *)findAcctBlncWithUserId:(NSString *)userId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]           = userId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_FINDACCTBLNC andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToObjectWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

-(NetworkResponse *)addMissedWithUserId:(NSString *)userId
                             businessId:(NSString *)businessId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"userId"]           = userId;
        params[@"businessId"]       = businessId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_ADDMISSED andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}


-(NetworkResponse *)addOnlineWorkWithOnlineWork:(OnlineWork *)onlineWork{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"onlineWork"]           = onlineWork;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_ADDONLINEWORK andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

/**
 * @pragmas type:1.good 2.bad
 */
-(NetworkResponse *)addBusinessEvalWithBusinessId:(NSString *) businessId userId:(NSString *) userId type:(NSString *)type workId:(NSString *) workId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"businessId"]           = businessId;
        params[@"userId"]               = userId;
        params[@"type"]                 = type;
        params[@"workId"]               = workId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_ADDBUSINESSEVAL andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}


-(NetworkResponse *)addPraiseCountWithExperienceId:(NSString *) experienceId{
    NetworkAccess *access = [[NetworkAccess alloc]init];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    @try {
        params[@"experienceId"]           = experienceId;
    }
    @catch (NSException *exception) {
        NSLog(@"kyson:%@",exception);
    }
    
    NetworkResponse * response = [access doHttpRequest:URL_ADDPRAISECOUNT andParams:params];
    
    if (0 == response.errorCode) {
        [JsonHandler convertToBoolWithResponse:&response];
    }else{
        NSLog(@"error message:%@",response.errorMessage);
    }
    
    return response;
}

/*******************************************about thread************************************************/

-(void)setServiceName:(NSString *)serviceName{
    _serviceName = serviceName;
    if ([serviceName isEqual:SERVICE_DOLOGIN]) {
        _serviceUrl = URL_DOLOGIN;
        self.paramsList = [NSArray arrayWithObjects:@"userAcct",@"userPwd", nil];
    }else if ([serviceName isEqual:SERVICE_FINDOFFLINEWORK]){
        self.paramsList = [NSArray arrayWithObjects:@"infoTypeId",@"cityId",@"countyId",@"pageIndex",@"pageSize", nil];
    }else if ([serviceName isEqual:SERVICE_FINDEXPERIENCESHARE]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",@"pageIndex",@"pageSize", nil];
    }else if ([serviceName isEqual:SERVICE_FINDEXPERIENCESHAREEVAL]){
        self.paramsList = [NSArray arrayWithObjects:@"experienceId", nil];
    }else if ([serviceName isEqual:SERVICE_MYPUBLISHLIST]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",@"pageIndex",@"pageSize", nil];
    }else if ([serviceName isEqual:SERVICE_FINDSIGNUPSTUDENT]){
        self.paramsList = [NSArray arrayWithObjects:@"workId",@"businessId", nil];
    }else if ([serviceName isEqual:SERVICE_UPDATEUSERCBTN]){
        self.paramsList = [NSArray arrayWithObjects:@"userIdCbtn",@"workId", nil];
    }else if ([serviceName isEqual:SERVICE_MYSHOULDHIRELIST]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",@"pageIndex",@"pageSize", nil];
    }else if ([serviceName isEqual:SERVICE_FINDSTUDENTINFO]){
        self.paramsList = [NSArray arrayWithObjects:@"userId", nil];
    }else if ([serviceName isEqual:SERVICE_FINDINCOMEEXPN]){
        self.paramsList = [NSArray arrayWithObjects:@"userId", nil];
    }else if ([serviceName isEqual:SERVICE_UPDATEONLINESIGNUP]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",@"workId", nil];
    }else if ([serviceName isEqual:SERVICE_ADDOFFLINEWORK]){
        self.paramsList = [NSArray arrayWithObjects:@"work", nil];
    }else if ([serviceName isEqual:SERVICE_UPDATESTUDENTINFO]){
        self.paramsList = [NSArray arrayWithObjects:@"student", nil];
    }else if ([serviceName isEqual:SERVICE_ADDEXPRSHARE]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",@"content", nil];
    }else if ([serviceName isEqual:SERVICE_FINDACCTBLNC]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",nil];
    }else if ([serviceName isEqual:SERVICE_ADDMISSED]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",@"businessId",nil];
    }else if ([serviceName isEqual:SERVICE_ADDONLINEWORK]){
        self.paramsList = [NSArray arrayWithObjects:@"onlineWork",nil];
    }else if ([serviceName isEqual:SERVICE_ADDBUSINESSEVAL]){
        self.paramsList = [NSArray arrayWithObjects:@"businessId",@"userId",@"type",@"workId",nil];
    }else if ([serviceName isEqual:SERVICE_ADDPRAISECOUNT]){
        self.paramsList = [NSArray arrayWithObjects:@"experienceId",nil];
    }else if ([serviceName isEqual:SERVICE_ADDEXPRSHAREEVAL]){
        self.paramsList = [NSArray arrayWithObjects:@"userId",@"content",@"experienceId",nil];
    }else if ([serviceName isEqual:SERVICE_DOREGISTER]){
        self.paramsList = [NSArray arrayWithObjects:@"userAcct",@"userPwd",@"validateCode",nil];
    }else if ([serviceName isEqual:SERVICE_DOSMSCODE]){
        self.paramsList = [NSArray arrayWithObjects:@"userAcct",@"type",nil];
    }else {
//        @throw [[NSException alloc]initWithName:@"no params" reason:@"no params" userInfo:nil];
        NSLog(@"params have not been setted!");
    }
    
}



-(void)main{
    NSString *method = self.serviceName;
    SEL service =NSSelectorFromString(method);
    NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:service];
    
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
    [invocation setTarget:self];
    [invocation setSelector:service];
    Model *model = [Model shareInstance];
    if (_paramsList && [_paramsList count] > 0) {
        NSInteger count = [_paramsList count];
        //set params
        for (int i = 0 ; i < count ; ++i) {
            NSString *keyItem = _paramsList [i];
            NSString *paramValue = [model valueForKey:keyItem];
            if (paramValue) {
                [invocation setArgument:&paramValue atIndex:i+2];
            }else{
                NSLog(@"kyson:params %@ is nil,please set it on the model",keyItem);
            }
        }
    }else{
        NSLog(@"there is no params is found");
    }
    
    [invocation retainArguments];
    
    //invoke method
    NetworkResponse *__autoreleasing response = nil;
    
    [invocation invoke];
    [invocation getReturnValue:&response];
    
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(serviceMediator:resultDidFetched:)]) {
        if (response) {
            [self.mDelegate serviceMediator:self resultDidFetched:response];
        }else{
            NSLog(@"######");
            NSLog(@"%@: no response",self);
            NSLog(@"######");
            
        }
    }else {
        NSLog(@"%@:please set delegate",self);
    }
    
}



@end
